const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();

    //
    //开始渲染后台首页
    router.use('/forumbinfo.html',function(req,res,next){
        var delelt= + req.query.forumBId;

        //格式化url栏的参数，转换为对像
        //用户确定要删除，查询当前对应的是否有商品,没有商品就删除成功
        if(urlobj.parse(req.url,true).query.act==null||urlobj.parse(req.url,true).query.act=="")
        {
            db.query('SELECT * FROM forumbinfo,forumainfo,userinfo where userinfo.userId=forumbinfo.userId and forumbinfo.forumAId=forumainfo.forumAId',function (err,data){
                if(err){
                    res.status(400).send('<script>alert("程序出错");window.location.href="400.html";</script>')
                }
                else{
                    if(data.length==0){
                        res.status(400).send('<script>alert("暂无数据");window.location.href="400.html";</script>')
                    }
                    else{
                        req.msg=data;
                        res.render("forum/forumbinfo.ejs",{msg:req.msg});
                    }
                }
            });
        }
        else if(urlobj.parse(req.url,true).query.act=="delete")
        {
            db.query("SELECT * FROM forumbinfo,forumcomment WHERE forumbinfo.forumBId=forumcomment.forumBId and forumbinfo.forumBId="+parseInt(req.query.forumBId),function(err,data) {
               console.log(err);
                if(err)
                {
                    res.send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
                }else
                {

                    if(data.length==0)
                    {
                        console.log(5);
                        db.query("DELETE FROM forumBinfo WHERE forumBId="+parseInt(req.query.forumBId),function(err,data) {
                            if(err)
                            {
                                res.status(500).send('<script>alert("该分类下游数据!");window.location.href="forumbinfo.html";</script>').end();
                            }else
                            {
                                res.status(200).send('<script>alert("删除成功!");window.location.href="forumbinfo.html";</script>').end();
                            }
                        });

                    }else{
                        res.send('<script>alert("该分类下游数据!");window.location.href="forumbinfo.html";</script>').end();
                    }
                }
            });
        }

    });

    return router;
}
